<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 反馈
 *
 */
class Feedback extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		//加载usermodel
		$this->load->model('usermodel');
		$this->uid = $this->usermodel->get_id_by_token($this->input->post('token'));
		$this->load->model('feedbackmodel');
		//加载表单验证类库
  		$this->load->library('form_validation');
  		//加载ualib
		$this->load->library('ualib');
	}
	
	function index()
	{
		header('Content-type:text/xml');// 向浏览器输出 xml
		//返回空值
		echo $this->xmllib->empty_xml();
		exit;
	}
	
	/**
	 * 添加意见反馈
	 *
	 */
	function addfeedback()
	{
		header('Content-type:text/xml');// 向浏览器输出 xml
		$_POST['content'] = isset($_POST['content']) ? $_POST['content'] : '';//此处设置时为了防止生成xml报错
		//字段验证设置
		$this->form_validation->set_rules('content', '意见反馈内容', 'trim|required|max_length[250]');
		$this->form_validation->set_message('required', '%s不能为空');
		$this->form_validation->set_message('max_length', '%s字数不能多于%d');
		$this->form_validation->set_error_delimiters('', '');
		if ($this->form_validation->run() == FALSE){
			//显示错误
			echo $this->xmllib->error_xml(form_error('content'));
			exit;
		}else{
			$content = $this->input->post('content');
			$data = array('uid' => $this->uid, 'content' => $content);
			//ua信息
			$user_agent = $this->ualib->fetch_array();
			unset($user_agent['unionid']);//删除不需要存储的ua部分信息
			$data = array_merge($data, $user_agent);
			if($this->feedbackmodel->insert_feedback($data)){
				//添加成功
				echo $this->xmllib->success_xml("您的意见已提交成功，感谢您的参与");
				exit;
			}else{
				echo $this->xmllib->error_xml("意见反馈提交失败，请稍候再试");
				exit;
			}
		}
	}
}